package basic.lessons.mosaic;

import java.awt.Color;

public class MosaicSquaresGrow {
	/*
	 * In this applet, all the squares start out with the default color, black.
	 * Every time the disturbance visits a square, a small amount is added to
	 * the red component of the color of that square. Write a subroutine that
	 * will add 25 to the red component of one of the squares in the mosaic. The
	 * row and column numbers of the square should be passed as parameters to
	 * the subroutine. Recall that you can discover the current red component of
	 * the square in row r and column c with the function call
	 * Mosaic.getRed(r,c)
	 */
	   
	   final static int ROWS = 41;        // Number of rows in the mosaic.
	   final static int COLUMNS = 41;     // Number of columns in the mosaic.
	   final static int SQUARE_SIZE = 5; // Size of each square in the mosaic.
	       
	   static int currentRow; // row currently containing the disturbance
	   static int currentColumn; // column currently containing disturbance
	   static int squareNumber;
	   static int colorChange;
	   
	   
	           
	   public static void main(String[] args) {
	          // Main program creates the window, fills it with
	          // random colors, then moves the disturbance in
	          // a random walk around the window.
	       Mosaic.open(ROWS, COLUMNS, SQUARE_SIZE, SQUARE_SIZE);
	       Mosaic.fill(Color.black);
	       currentRow = 20;   // start at center of window
	       currentColumn = 20;
	       squareNumber = 1;
	       while (Mosaic.isOpen()) {	    	   
	    	   drawSquare(50+colorChange, 50+colorChange, 50+colorChange);
	    	   Mosaic.delay(50);
	    	   drawSquare(0, 0, 0);
	    	   moveCurrentCell();
	       }
	   }  // end of main()
	   
   
	static void drawSquare(int red, int green, int blue) {
		int topRow = currentRow;
		int leftColumn = currentColumn;
		int squareWidth =2*squareNumber-1;

			for (int cellCount = 0; cellCount <squareWidth; cellCount++) {
				Mosaic.setColor(topRow, leftColumn + cellCount, red, green,
						blue); // fill the top row
				Mosaic.setColor(topRow + squareWidth-1, leftColumn
						+ cellCount, red, green, blue); // fill the bottom row
				Mosaic.setColor(topRow + cellCount, leftColumn, red, green,
						blue); // fill the left column row
				Mosaic.setColor(topRow + cellCount, leftColumn + squareWidth-1
						, red, green, blue); // fill the right row
			}

		} 
	   

	   static void moveCurrentCell (){
		   currentRow --;
		   currentColumn --; 
		   squareNumber ++;
		   colorChange += 10;
			if (currentRow <0 || currentColumn <0) {
			currentRow = 20;
			currentColumn = 20;
			squareNumber = 1;
			colorChange = 0;
		}

	   }
	   
	
	} // end of class RandomMosaicWalk2